package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.wms.dto.ProcessDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wms.domain.Process;
import com.ruoyi.wms.service.IProcessService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工序管理Controller
 *
 * @author ruoyi
 * @date 2025-10-16
 */
@Api(value = "工序管理Controller", tags = "工序管理Controller")

@RestController
@RequestMapping("/process")
public class ProcessController extends BaseController {
    @Autowired
    private IProcessService processService;

    /**
     * 查询工序管理列表
     */
    @RequiresPermissions("wms:process:list")
    @GetMapping("/list")
    public TableDataInfo list(ProcessDto process) {
        startPage();
        List<Process> list = processService.selectProcessList(process);
        return getDataTable(list);
    }


    /**
     * 获取工序管理详细信息
     */
    @RequiresPermissions("wms:process:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(processService.selectProcessById(id));
    }

    /**
     * 修改工序管理
     */
    @ApiOperation("修改工序")
    @RequiresPermissions("wms:process:edit")
    @Log(title = "工序管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Process process) {
        return toAjax(processService.updateProcess(process));
    }

    /**
     * 删除工序管理
     */
    @RequiresPermissions("wms:process:remove")
    @Log(title = "工序管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(processService.deleteProcessByIds(ids));
    }


    @ApiOperation("确认工序,根据人名字查工序")
    @GetMapping(value = "/queryProcessByDismantler")
    public AjaxResult queryProcessByDismantler(@ApiParam(value = "拆解员", required = true) @RequestParam(value = "dismantler") String dismantler) {
        ProcessDto pro = new ProcessDto();
        pro.setDismantler(dismantler);
        return success(processService.queryProcessByDismantler(pro));
    }


    @ApiOperation("pda调整工序")
    @PostMapping(value = "/adjustPprocess")
    public AjaxResult adjustPprocess(
            @ApiParam(value = "前工序的id", required = true) @RequestParam(value = "processId1") String processId1,
            @ApiParam(value = "前工序的拆解员", required = true) @RequestParam(value = "dismantler1") String[] dismantlerArr1,
            @ApiParam(value = "后工序的id", required = true) @RequestParam(value = "processId2") String processId2,
            @ApiParam(value = "后工序的拆解员", required = true) @RequestParam(value = "dismantler2") String[] dismantlerArr2

    ) {
        Process pro = new Process();
        pro.setId( processId1);
        pro.setDismantler(String.valueOf(String.join("|", dismantlerArr1) )     );
        int result = processService.updateProcessInfo(pro);


        Process pro2 = new Process();
        pro2.setId( processId2);
        pro2.setDismantler( String.valueOf(String.join("|", dismantlerArr2) ) );
        result = processService.updateProcessInfo(pro2);

        pro=pro2=null;
        return success(result);
    }


}
